package com.situ.achievementsystem.project.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.achievementsystem.project.model.ProjectModel;
import com.situ.achievementsystem.project.service.ProjectService;

/**
 * Servlet implementation class ProjectServlet
 */
@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	doPost(request, response);
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		ProjectModel model = null;
		Object res = "";
		
		switch (action) {
		case "list":
			model = new ProjectModel(request.getParameter("code"),request.getParameter("name"));
			res = service.selectList(model);
			res = new JSONArray((Collection<?>)res).toString();
			break;

		case "add":
			model = new ProjectModel(request.getParameter("code"),request.getParameter("name"));
			res = service.insert(model);
			break;
			
		case "del":
			model = new ProjectModel(request.getParameter("code"));
			res = service.delete(model);
			break;
			
		case "get":
			model = new ProjectModel(request.getParameter("code"));
			res = service.selectModel(model);
			res = new JSONObject(res).toString();
			break;
			
		case "upd":
			model = new ProjectModel(request.getParameter("code"),request.getParameter("name"));
			res = service.update(model);
			break;
			
		case "page":
			model = new ProjectModel(request.getParameter("code"),request.getParameter("name"));
//			res = service.selectList(model);
//			res = new JSONArray((Collection<?>)res).toString();
			
	        model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
	        model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
	        model.setPageOn(true);
	        Map<String, Object> map = new HashMap<>();
	        map.put("data", service.selectList(model));
	        map.put("count", service.selectCount(model));
	        res = map;
	        res = new JSONObject((Map<?,?>)res).toString();
			break;
			
		default:
			break;
		}
		
		PrintWriter wr = response.getWriter();
		wr.print(res);
		wr.flush();
		wr.close();
		wr=null;
	}
	
	private ProjectService service = new ProjectService();

}
